package com.vgulu.server.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Administrator on 2014/5/14 0014.
 */
public class CacheUtil {

    private final static Logger logger = LoggerFactory.getLogger(CacheUtil.class);

    private  static JedisPool jedisPool  ;
    private final static int threadCount = 100;
    private static String prefix;

    private CacheUtil(){};

    static {
        try{
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxIdle(threadCount);
            poolConfig.setMaxTotal(threadCount);
            poolConfig.setTimeBetweenEvictionRunsMillis(-1);
            poolConfig.setTestOnBorrow(true);
            String host = PropertyConfigurer.getPropertyAsString("redis.host");
            int port = PropertyConfigurer.getPropertyAsInt("redis.port");
            int timeOut = PropertyConfigurer.getPropertyAsInt("redis.timeout.secs");
            prefix = PropertyConfigurer.getPropertyAsString("redis.key.prefix");
            if(StringUtils.isEmpty(PropertyConfigurer.getPropertyAsString("redis.pwd"))){
                jedisPool = new JedisPool(poolConfig,host,port,timeOut*1000);
            }else {
                jedisPool = new JedisPool(poolConfig,host,port,timeOut*1000,PropertyConfigurer.getPropertyAsString("redis.pwd"));
            }

        }catch (Exception e){
            logger.error("redis init error",e);
        }
    }



    public static boolean set(String key,String value,int expireTimeInSec ){

        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.setex(prefix+key,expireTimeInSec,value);
            return true;
        }catch (Exception e){
            logger.error("redis set with expire time error", e);
        }finally {
            jedis.close();
        }
        return false;
    }

    public static void set(String key,String value){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.set(prefix+key,value);
        }catch (Exception e){
            logger.error("redis set error", e);
        }finally {
            jedis.close();
        }
    }

    public static String get(String key){
        Jedis jedis = null;
        String value = null;
        try{
            jedis = jedisPool.getResource();
            value = jedis.get(prefix+key);
        }catch (Exception e){
            logger.error("redis get error",e);
        }finally {
            jedis.close();
        }
        return  value;
    }

    public static void del(String key){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.del(prefix+key);
        }catch (Exception e){
            logger.error("redis del error", e);
        }finally {
            jedis.close();
        }
    }

    public static void expire(String key,int seconds){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.expire(prefix+key,seconds);
        }catch (Exception e){
            logger.error("redis expire error", e);
        }finally {
            jedis.close();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CacheUtil.set("89$APP", "xuege", 5);
        System.out.println(CacheUtil.get("89$APP"));
        Thread.sleep(4 * 1000);
        System.out.println(CacheUtil.get("89$APP"));
       // CacheUtil.expire("89$APP",5);
        Thread.sleep(4 * 1000);
        System.out.println(CacheUtil.get("89$APP"));
    }
}
